package com.sg.web;

import com.github.pagehelper.PageInfo;
import com.sg.bean.*;
import com.sg.service.BusinessService;
import com.sg.service.BusinessStateService;
import com.sg.service.CommodityCateGorpService;
import com.sg.service.ShopService;
import com.sg.util.NewFile;
import com.sg.vo.BusinessVO;
import com.sg.vo.ShopVO;
import com.sg.vo.UserVO;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * @author wangcf
 * @Description 商家管理控制器
 * @date 2023/3/14 17:38
 */
@Controller
public class BusinessController {

    @Autowired
    private BusinessService businessService;

    @Autowired
    private BusinessStateService businessStateService;
    @Autowired
    private CommodityCateGorpService commodityCateGorpService;

    @Autowired
    private ShopService shopService;
    Logger logger = Logger.getLogger(BusinessController.class);

    @RequestMapping("businessindex")
    public String businessindex(int businessId, Model model, HttpSession session) {
        logger.info("进入商品主页，把分类列表显示控制器");
        Business business = businessService.findById(businessId);//根据商家id查到商家的信息
        List<CommodityCateGorp> commodityCateGorps = commodityCateGorpService.findByBusinessId(businessId);//根据商家编号查询商家所有的商品分类
        logger.info(commodityCateGorps);
        //根据商家编号和用户id查询购物车
        Object obj = session.getAttribute("createUser");
        UserVO user = (UserVO) obj;
        if (user != null) {
            List<ShopVO> list = shopService.findByUidAndBusinessId(user.getuId(), businessId);//查到购物车所有商品
            model.addAttribute("shopcommoditylist", list);//显示给用户
            Shop shop = shopService.findByUidAndBusinessIdShop(user.getuId(), businessId);
            model.addAttribute("shop", shop);
        }
        model.addAttribute("business", business);//传到前端页面
        model.addAttribute("commoditycategorplist", commodityCateGorps);
        return "businessindex";
    }

    //分页显示
    @RequestMapping("businesslist")
    public String businesslist(String pageNum, @RequestParam(defaultValue = "5") int pageSize, Model model) {
        logger.info("进到商家列表分页控制器");
        //1.获取当前页码，和查询条件
        //2.判断首次进入时，点击用户管理，默认当前页码是1，查询条件是null
        if (pageNum == null || "".equals(pageNum)) {
            pageNum = "1";
        }
        //查询到一个集合,pageNum页码pageSize
        List<BusinessVO> voList = businessService.findAllPage(Integer.parseInt(pageNum), pageSize);
        //4.把集合数据封装到一个对象page中
        PageInfo pageInfo = new PageInfo(voList);
        Page page = new Page();
        page.setPageInfo(pageInfo);
        //动态地址和动态查询条件的封装，用于回显
        page.setServletUrl("/businesslist.action");
        //把page对象储存到Model中，在当前页面取出
        model.addAttribute("page", page);
        //返回jsp页面
        return "businesslist";
    }

    @RequestMapping("/admin/businesstel")
    public @ResponseBody boolean businesstel(String businessTel) {
        logger.info("进入商家电话验证控制器了");
        logger.info(businessTel);
        boolean i = businessService.findByTel(businessTel);
        return i;
    }

    @RequestMapping("/admin/tobusinessadd")
    public String tobusinessadd(Model model) {
        logger.info("进入添加回显控制器");
        List<BusinessState> states = businessStateService.findAll();
        model.addAttribute("businessstatelist", states);
        return "admin/businessadd";
    }

    @RequestMapping("/admin/businessadd")
    public String businessadd(Business business) {
        logger.info("进入到添加商家控制器");
        logger.info(business);
        boolean b = businessService.add(business);
        logger.info(b);
        return "redirect:../businesslist.action";
    }

    @RequestMapping("/admin/businessupdatestate")
    public String businessupdatestate(Business business) {
        logger.info("进入到恢复商家营业控制器");
        logger.info(business);
        boolean i = businessService.update(business);
        logger.info(i);
        return "redirect:../businesslist.action";
    }

    @RequestMapping("tothirdlylogin")
    public String tothirdlylogin() {
        return "thirdlylogin";
    }

    @RequestMapping("thirdlylogin")
    public String thirdlylogin(String captcha, Business business, HttpSession session, Model model) {
        logger.info("进入外卖商家登录控制器");
        //获取session里的验证码，判断与用户输入的是否相同
        String simpleCaptcha = (String) session.getAttribute("simpleCaptcha");
        logger.info(simpleCaptcha);
        model.addAttribute("businesstel", business.getBusinessTel());
        if (simpleCaptcha.equals(captcha)) {
            BusinessVO business1 = businessService.findByTelAadPass(business);
            logger.info(business1);
            if (business1 != null) {
                session.setAttribute("business", business1);
                logger.info("外卖商家登录成功");
                return "redirect:businessindex.action?businessId=" + business1.getBusinessId();
            } else {
                logger.info("登录失败");
                model.addAttribute("mess", "密码或账号错误");
                return "thirdlylogin";
            }
        } else {
            model.addAttribute("mess", "验证码输入错误，请重新输入");
            logger.info("验证码错误，没登上去");
            return "thirdlylogin";
        }
    }

    @RequestMapping("thirdly/tobusinessinfo")
    public String businessinfo(Model model) {
        List<BusinessState> states = businessStateService.findAll();
        model.addAttribute("businessstatelist", states);
        return "thirdly/businessinfo";
    }

    @RequestMapping("thirdly/businessupdate")
    public String businessupdate(HttpSession session,Business business, @RequestParam("file1") CommonsMultipartFile file1, @RequestParam("file2") CommonsMultipartFile file2) throws IOException {
        logger.info("进入商家修改信息控制器");
        logger.info(business);
        if (file1 != null && file1.getSize() > 0) {
            File newFile = NewFile.getFile(file1);
            logger.info("logo"+newFile);
            file1.transferTo(newFile);//上传到目标位置
            business.setBusinessLogo(NewFile.getNewFileNameAnd(newFile.getName()));//设置商家logo
            logger.info(business.getBusinessLogo());
        }
        if (file2 != null && file2.getSize() > 0) {
            File newFile = NewFile.getFile(file2);
            logger.info("img"+newFile);
            file2.transferTo(newFile);//上传到目标位置
            business.setBusinessImg(NewFile.getNewFileNameAnd(newFile.getName()));//设置商家logo
            logger.info(business.getBusinessLogo());
        }
        boolean i = businessService.update(business);
        logger.info(i);
        Object obj = session.getAttribute("business");
        BusinessVO business1 = (BusinessVO) obj;
        return "redirect:../businessindex.action?businessId=" + business.getBusinessId();
    }

}
